package everyday;

import org.junit.jupiter.api.Test;

public class T1330 {
    public int maxValueAfterReverse(int[] nums) {

        int sum = 0;
        int add = 0;
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
        int n = nums.length;
        for (int i = 0; i < n - 1; i++) {
            int diff = Math.abs(nums[i] - nums[i + 1]);
            sum += diff;
            max = Math.max(max, Math.min(nums[i], nums[i + 1]));
            min = Math.min(min, Math.max(nums[i], nums[i + 1]));
        }
//        System.out.println(sum+"  "+max + " " + min);
        add = Math.max(Math.max(Math.max(max - nums[0], nums[0] - min), Math.max(max - nums[n - 1], nums[n - 1] - min)), 0);
        if (max > min) {
            add = Math.max(add, 2 * (max - min));
        }
        sum += add;

        return sum;
    }

    @Test
    public void test() {
        int[] arr = {2, 5, 1, 3, 4};
        System.out.println(maxValueAfterReverse(arr));
    }
}
